/*
 * Copyright (c) 2012 Universidad Antonio de Nebrija.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0.html
 * 
 * Contributors:
 *    Universidad Antonio de Nebrija - initial API, implementation and documentation
 */
package com.nebrija.riif.cli;

import java.util.ArrayList;

import com.nebrija.riif.model.RootClass;
import com.nebrija.riif.model.StepTimelineExecution;
import com.nebrija.riif.model.Timeline;
import com.nebrija.riif.model.RealTimelineExecution;
import com.nebrija.riif.model.TimelineExecution;

/**
 * Command to resolve the cross-references in the property values
 * @author asanchep
 *
 */
public class RunTimelineCommand implements Command {

	@Override
	public String execute(ArrayList<String> params) throws CommandException {
		RootClass r = RootClass.getInstance();
		// Resolution must be done 
		r.resolve(null);
		Timeline t = r.getGlobalTimeline();
		TimelineExecution exec = new RealTimelineExecution(t);
		if(params.size()!=0){
			if (params.get(0).equals("step")){
				exec = new StepTimelineExecution(t); // Create step by step class
			}
		}
		if(t==null){
			return "Error. A valid timeline must be set as global";
		}
		
		t.setExecution(exec);
		exec.runTimeline();
		r.resolve(null);
		return "Execution started for timeline " + t.getName();
		
	}

	@Override
	public String helpString() throws CommandException {
		return " <step/real>: run a timeline in a real or step by step way";
	}

}
